package com.meiyu.live.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;

import com.android.volley.Response;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMShareAPI;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.BaseApplication;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.net.param.MysettingParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.util.RippleUtils;
import com.meiyu.live.util.ToastUtil;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zbl on 2016/8/23.
 */
public class SettingsActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private UMShareAPI mShareAPI;

    private View btn_back, btn_logout, btn_clear_cache;
    private SwitchCompat sw_followanchor_startlive;

    private boolean isLoadingData = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        mShareAPI = UMShareAPI.get(this);
        init();
    }

    private void init() {
        int mainColor = getResources().getColor(R.color.main_theme_color);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_clear_cache = findViewById(R.id.btn_clear_cache);
        btn_clear_cache.setOnClickListener(this);
        btn_logout = findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(this);
        RippleUtils.setRippleDrawable(btn_clear_cache, mainColor);
        RippleUtils.setRippleDrawable(btn_logout, mainColor);
        btn_logout.setVisibility(UserConfig.getInstance().isLogin() ? View.VISIBLE : View.GONE);

        sw_followanchor_startlive = (SwitchCompat) findViewById(R.id.sw_followanchor_startlive);
        if (UserConfig.getInstance().getPersonalInfo() != null) {
            sw_followanchor_startlive.setChecked(UserConfig.getInstance().getPersonalInfo().getAnchor_push());
        } else {
            sw_followanchor_startlive.setChecked(false);
        }

        sw_followanchor_startlive.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        } else if (view == btn_clear_cache) {
            clearCache();
        } else if (view == btn_logout) {
            BaseApplication.getApp().logout(false);
            finish();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {
        if (compoundButton == sw_followanchor_startlive) {
            request(new MysettingParams(b, JPushInterface.getRegistrationID(getApplicationContext())), BaseResponse.class, new Response.Listener<BaseResponse>() {
                @Override
                public void onResponse(BaseResponse onlyStatusResponse) {
                    if (onlyStatusResponse.getStatusCode() == Constant.StatusCode_Success) {
                        UserConfig.getInstance().getPersonalInfo().setAnchor_push(b);
                    } else {
                        ToastUtil.show("关闭失败，请稍后再试");
                        sw_followanchor_startlive.setChecked(UserConfig.getInstance().getPersonalInfo().getAnchor_push());
                    }
                }
            });
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onError() {
        super.onError();
    }

    private void clearCache() {
        ImageLoader.getInstance().clearDiskCache();
        ToastUtil.show("清理完成！");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }
}
